%main = draw(main,pos,rings)
%   Using the information stored in the structs 'main' and 'rings' this
%   function calculates the coordinates the atoms have to be printed at for
%   the 2D picture. It stores these coordinates into the fields 'x' and 'y'
%   in an edited version of 'main' which it outputs

%%

function main = draw(main,pos,rings)
%Inputted main need to have ringmarkers of which none are empty!

%% Initialize

%The function needs the fields 'drawn', 'ring' and 'hasBranch' to exist
%within the struct 'main' in order to be able to proceed.
if ~myIsField(main,'drawn')
    for j = 1:length(main)
        main(j).drawn = 0;
    end
end

if ~myIsField(main,'ring')
    for j = 1:length(main)
        main(j).ring = [];
    end
end

if ~myIsField(main,'hasBranch')
    for j = 1:length(main)
        main(j).hasBranch = [];
    end
end

%If this is the first position, set starting position
if pos == 1
    main(pos).x = 1;
    main(pos).y = 0;
    main(pos).drawn = 1;
end

%% Main
%If this is a ringmember
if main(pos).ring > 0 %main(pos).ringmarker ~= 0
    %And at least one of it's rings members' coordinates are known
    drawnSum = sum([main([rings(main(pos).ring).elements]).drawn]);
    if  drawnSum > 0 && drawnSum < length([rings(main(pos).ring).elements])
        %Calculate the rings' coordinates
        main = drawRing(main,pos,rings);
    end
end

%If the current has no definite coordinates yet
if ~main(pos).drawn
    %Set 'drawn' to one
    main(pos).drawn = 1;
    
    %If the current has a branchorigin and it is the first of the branch
    ori = main(pos).branchorigin;
    if ori && pos == find([main.branch] == main(pos).branch,1)
        %The 'drawn' of the origin is increased
        main(ori).drawn = main(ori).drawn + 1;
    end
    
    %Draw the current position
    %If first of branch
    if pos == find([main.branch] == main(pos).branch,1)
        %If the origin has a branchorigin and it's the first of it's branch
        if main(ori).branchorigin && ori == find([main.branch] ...
                == main(ori).branch,1)
            %previous is the origin of the origin
            prev = main(ori).branchorigin;
        else
            %previous is the last element of the branch in which origin
            %resides
            temp = find([main.drawn] == 1);
            prev = temp(find([main(find([main.drawn] == 1)).branch] ...
                == main(ori).branch,1,'last'));
        end
        %'base' is the vector from the origin the current is bound to, to
        %the atom the origin is bound to
        if prev ~= 0
            base = [main(prev).x main(prev).y] - [main(ori).x main(ori).y];
        else
            base = -[main(1).x main(1).y];
        end
        
        %If we were planning on adding the current on the position we want
        %to reserve for the next-nonbranch atom attached to 'ori' then the
        %multiplier is set to the maximum. In this case it is the
        %coordination of the 'ori'.
        %This is overruled by the E/Z configurations.
        if main(ori).dubbondconfig ~= 'E'
            mult = 2;
        else
            if round(main(ori).coordination/2) == main(ori).drawn-1
                if round(main(ori).coordination/2) == ...
                        main(ori).coordination -1
                    mult = main(ori).coordination -2;
                else
                    mult = main(ori).coordination-1;
                end
            else
                %Else the multiplier is set to the current drawn amount for
                %the 'ori'.
                mult = main(ori).drawn-1;
            end
        end
        
        %Angle between previous drawn bond and new bond:
        angle = 360/main(ori).coordination;
        %Vector to be added to previous coordinates
        add = [cosd(angle) -sind(angle);sind(angle) cosd(angle)]^mult...
            *base';
        %Storing coordinates
        main(pos).x = main(ori).x+add(1);
        main(pos).y = main(ori).y+add(2);
    else
        branchmembers = find([main.branch] == main(pos).branch);
        branchpos = find(branchmembers == pos);
        %If the previous atom in the current branch is the first of the 
        %branch
        if branchpos == 2
            %'prev' is the origin of the previous
            prev = main(branchmembers(1)).branchorigin;
        else
            %'prev' is the element branchmembers(branchpos-2)
            prev = branchmembers(branchpos-2);
        end
        
        %'base' is the vector from the origin the current is bound to, to
        %the atom the origin is bound to
        %If no 'prev' has really been found assign a 'base' of the negative
        %coordinates of the first element, else assign the vector between
        %the atom this one is going to be bound to and 'prev'.
        if prev ~= 0
            if prev == branchpos-1 && prev == 1
                base = -[main(1).x main(1).y];
            else
                base = [main(prev).x main(prev).y] - ...
                    [main(branchmembers(branchpos-1)).x ...
                    main(branchmembers(branchpos-1)).y];
            end
        else
            base = -[main(1).x main(1).y];
        end
        %Getting the correct multiplier for the reserverved spot
        if main(branchmembers(branchpos-1)).dubbondconfig == 'E'
            mult = 1;
        else
            mult = round(main(branchmembers(branchpos-1)).coordination/2);
        end
        
        %Angle between previous drawn bond and new bond:
        angle = 360/main(branchmembers(branchpos-1)).coordination;
        %Vector to be added to previous coordinates
        add = [cosd(angle) -sind(angle);sind(angle) cosd(angle)]^mult...
            *base';
        %Storing coordinates
        main(pos).x = main(branchmembers(branchpos-1)).x+add(1);
        main(pos).y = main(branchmembers(branchpos-1)).y+add(2);        
    end
end

%If the current atom has branches, "draw" those
if ~isempty(main(pos).hasBranch)
    for j = 1:length(main(pos).hasBranch)
        main = draw(main,main(pos).hasBranch(j),rings);
    end
end

%If the last position has not yet been reached, "draw" the next
if pos+1 <= length(main)
    main = draw(main,pos+1,rings);
end


end